import { LinRouter, NotFound, disableLoading, } from 'lin-mizar';
import { groupRequired } from '../../middleware/jwt';
import {
  AddContentValidator,
  EditContentValidator,
  DeleteContentValidator,
} from '../../validator/content';
import { ContentService } from '../../service/content';
import { logger } from '../../middleware/logger';



const contentApi = new LinRouter({
  prefix: '/v1/content',
});


/**
 * 新增
 */

contentApi.linPost(
  'addContent',
  '/',
  {
    permission: '添加期刊内容',
    module: '内容管理',
    mount: true
  },
  logger({ template: '{user.username}新增了期刊内容' }),
  groupRequired,
  async ctx => {
  const v = await new AddContentValidator().validate(ctx)
  await ContentService.addContent(v.get('body'));
  ctx.success({
    msg: '期刊内容新增成功'
  })
});

/**
 * 查询列表
 */
contentApi.get('/', async ctx => {
  const contentList = await ContentService.getContentList();
  ctx.json(contentList);
})

/**
 * 编辑
 */
contentApi.linPut(
  'editContent',
  '/:id',
  {
    permission: '编辑期刊内容',
    module: '内容管理',
    mount: true
  },
  groupRequired,
  logger({ template: '{user.username}编辑了期刊内容' }),
  async ctx => {
  const v = await new EditContentValidator().validate(ctx);
  const id = v.get('path.id');
  const param = v.get('body');
  await ContentService.editContent(id, param);
  ctx.success({
    msg: '期刊内容编辑成功'
  })
})

/**
 * 删除
 */
contentApi.linDelete(
  'deleteContent',
  '/:id',
  {
    permission: '删除期刊内容',
    module: '内容管理',
    mount: true
  },
  logger({ template: '{user.username}删除了期刊内容' }),
  groupRequired,
  async ctx => {
  const v = await new DeleteContentValidator().validate(ctx);
  const id = v.get('path.id');
  const type = v.get('query.type');
  await ContentService.deleteContent(id, type);
  ctx.success({
    msg: '期刊内容删除成功'
  })
})


module.exports = { contentApi, [disableLoading]: false };
